package com.cn.wanxi.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @ClassName MessageController
 * @Description TODO
 * @Author JiJiang
 * @Date 2022/9/28 16:55
 * @Version 1.0
 */

@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    //交换机的名字
    // Exchange 的 名字，要和Config里的一致或和rabbitmq里的对应exchange名一样
    private static String WX_IN_HOME_TOPIC_NAME = "wx.in_home.topic";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String send(String message, String rk) {
        // 这里我给三个路由都发消息：rk为：wx.students.all,wx.teachers.all,wx.hr.lida.
        // 这里的exchange名，rk，bk，这些信息在设计阶段最好定好，不要弄太多，
        // 做成枚举或写到配置文件里，避免整个项目里到处都是这些名字的字符串，不理由维护
        log.info("start send message  .... ");
        sendStudentsMessages();
        sendTeachersMessages();
        sendHRMessages();
        log.info("send message the end .... ");
        return "send message the end ....";
    }


    private void sendStudentsMessages() {
        String message = "因为疫情，万息全体学员居家3天，期间上课采用线上方式，考勤不变";
        for (int i = 0; i < 5; i++) {
            if (i % 2 == 0) {
                String uuid = UUID.randomUUID().toString();
                message = uuid + ":S:" + message;
                // p1:交换器 name,p2:routing key,p3:消息对象
                rabbitTemplate.convertAndSend(WX_IN_HOME_TOPIC_NAME, "wx.students.all", message);
                log.info("sendStudentsMessages=> i:{}, message:{}", i, message);
            }
        }

    }

    private void sendTeachersMessages() {
        String message = "因为疫情，万息全体老师居家3天，期间注意安排自己各个班级的学员上课情况，考勤不变";
        for (int i = 0; i < 5; i++) {
            if (i % 4 == 0) {
                String uuid = UUID.randomUUID().toString();
                message = uuid + ":T:" + message;
                rabbitTemplate.convertAndSend(WX_IN_HOME_TOPIC_NAME, "wx.teachers.all", message);

                log.info("sendTeachersMessages=> i:{}, message:{}", i, message);
            }
        }
    }

    private void sendHRMessages() {
        String message = "万息人事注意，疫情全公司都居家办公，注意随时接收政府的新情况";
        for (int i = 0; i < 5; i++) {
            if (i % 3 == 0) {
                String uuid = UUID.randomUUID().toString();
                message = uuid + ":HR:" + message;
                rabbitTemplate.convertAndSend(WX_IN_HOME_TOPIC_NAME, "wx.hr.lida", message);
                log.info("sendHRMessages=> i:{}, message:{}", i, message);

            }
        }
    }
}
